#define _CRT_SECURE_NO_WARNINGS 1


#include<vector>
#include<set>
#include<iostream>

using namespace std;

void BackTrack(vector<int>& nums, vector<vector<int>>& v, int n, int start, set<vector<int>>& tree)
{
	if (start == n)
	{
		if (tree.count(nums))
		{
			return;
		}
		v.push_back(nums);
		tree.insert(nums);
		return;
	}
	for (int i = start; i < n; i++)
	{
		BackTrack(nums, v, n, start + 1, tree);
		swap(nums[start], nums[i]);
		BackTrack(nums, v, n, start + 1, tree);
	}
}

vector<vector<int>> permute(vector<int>& nums)
{
	vector<vector<int>> v;
	set<vector<int>> tree;
	BackTrack(nums, v, nums.size(), 0, tree);
	return v;
}

int main()
{
	vector<int> v = { 1,2,3 };
	vector<vector<int>> ret = permute(v);
	for (int i = 0; i < ret.size(); i++)
	{
		for (int j = 0; j < ret[i].size(); j++)
		{
			cout << ret[i][j] << ' ';
		}
		cout << endl;
	}

	return 0;
}